import {chooseday,chooselist,bookday,Qrcode} from './request.js';
import '@babel/polyfill';
import '../css/index.css';
import '../css/select.css';
import '../css/return.css';
let _option = document.querySelector(".option_box");
let option_box = document.querySelectorAll(".option_box option");
// console.log(option_box);
let tow = document.querySelector(".tow")
let three = document.querySelector(".three")
let four = document.querySelector(".four")
let layui = document.querySelector("layui");
let day_time = [];
let value;
let res;

// 返回按钮
let returnbtn = document.querySelector(".return");
returnbtn.addEventListener("click",function () {
    if (sessionStorage.getItem("token")) {
        window.location.href = "./index.html";
    }else{
        alert("error!!!")
    }
})

if (sessionStorage.getItem("token")) {
    _option.addEventListener("change",async function () {
        tow.innerHTML = ""
        three.innerHTML = ""
        four.innerHTML = ""
        // ajax("http://newshopapi.0melon0.cn/api/f_user/chooseday", {
        //     "token": `${sessionStorage.getItem("token")}`,
        //     "day_time": _option.value,

        // }, "GET").then(re => {
        //     res = re;
        //     run();
        // })
        let data = {
            "token": `${sessionStorage.getItem("token")}`,
            "day_time": _option.value,
        }
        try {
            let re = await chooseday(data);
            res = re;
            run();
        } catch (error) {
            console.log(error);
        }
        // chooseday(data).then(re => {
        //     res = re;
        //     run();
        // })
    })
    // ajax("http://newshopapi.0melon0.cn/api/f_user/chooselist", {
    //     "token": `${sessionStorage.getItem("token")}`
    // }, "GET").then(res1 => {
    //     res1.result.list.forEach(function (item) {
    //         // _time.push(item.day_time)
    //         // console.log( item.day_time);
    //         _option.innerHTML += `<option value="${item.day_time}" id="${item.day_time}">${item.day_time}</option>`;
    //         day_time.push(item.day_time)
    //     })
    //     ajax("http://newshopapi.0melon0.cn/api/f_user/chooseday", {
    //         "token": `${sessionStorage.getItem("token")}`,
    //         "day_time": res1.result.list[0].day_time,
    //     }, "GET").then(re => {
    //         console.log(re);
    //         res = re;
    //         run();

    //     });
    // });
    let data = {
        "token": `${sessionStorage.getItem("token")}`
    }
    try {
        let res1 = await chooselist(data);
        res1.result.list.forEach(async function (item) {
            _option.innerHTML += 
            `<option value="${item.day_time}" id="${item.day_time}">${item.day_time}</option>`;
            day_time.push(item.day_time)
        })
        let data1 = {
            "token": `${sessionStorage.getItem("token")}`,
            "day_time": res1.result.list[0].day_time,
        }
        try {
            let re = await chooseday(data1)
            console.log(re);
            res = re;
            run()
        } catch (error) {
            console.log(error);
        }
    } catch (error) {
        console.log(error);
    }
    // chooselist(data).then(res1 => {
    //     res1.result.list.forEach(function (item) {
    //         // _time.push(item.day_time)
    //         // console.log( item.day_time);
    //         _option.innerHTML += `<option value="${item.day_time}" id="${item.day_time}">${item.day_time}</option>`;
    //         day_time.push(item.day_time)
    //     })
    //     let data1 = {
    //         "token": `${sessionStorage.getItem("token")}`,
    //         "day_time": res1.result.list[0].day_time,
    //     }
    //     chooseday(data1).then(re => {
    //         console.log(re);
    //         res = re;
    //         run()
    //     })
    // })

    let _tow = document.querySelector(".tow>span")
    let _three = document.querySelector(".three")
    let _four = document.querySelector(".four")
};
function run() {
    tow.innerHTML += `<span id="${res.result.list[0].show_id}">${res.result.list[0].show_name}</span>`;
    three.innerHTML += `<span id="${res.result.list[1].show_id}">${res.result.list[1].show_name}</span>`;
    four.innerHTML += `<span id="${res.result.list[2].show_id}">${res.result.list[2].show_name}</span>`;

    let timestamp = Date.now();
    let sessiontime0 = _option.value + " " + res.result.list[0].show_name.split("-")[0]
    let sessiontime1 = _option.value + " " + res.result.list[1].show_name.split("-")[0]
    let sessiontime2 = _option.value + " " + res.result.list[2].show_name.split("-")[0]

    let cctime0 = new Date(`${sessiontime0}`).getTime()
    let cctime1 = new Date(`${sessiontime1}`).getTime()
    let cctime2 = new Date(`${sessiontime2}`).getTime()

    if (cctime0 > timestamp) {
        tow.innerHTML += `<p>可预约</p>`
    } else {
        tow.innerHTML += `<p>不可预约</p>`

    }
    if (cctime1 > timestamp) {
        three.innerHTML += `<p>可预约</p>`
    } else {
        three.innerHTML += `<p>不可预约</p>`

    }
    if (cctime2 > timestamp) {
        four.innerHTML += `<p>可预约</p>`
    } else {
        four.innerHTML += `<p>不可预约</p>`
    }


    let _tow = document.querySelector(".tow>p")
    let _three = document.querySelector(".three>p")
    let _four = document.querySelector(".four>p")
    let _towspan = document.querySelector(".tow>span")
    let _threespan = document.querySelector(".three>span")
    let _fourspan = document.querySelector(".four>span")
   
    _tow.addEventListener("click",async function () {
        if (_tow.innerHTML == "可预约") {
            sessionStorage.setItem("day_time", `${_option.value}`);
            sessionStorage.setItem("cc_time", `${_towspan.innerHTML}`);
//1.0
                // console.log(_towspan.innerHTML);
                // console.log(_option.value);
                // console.log(_towspan.id);
            // ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
            //     "token": `${sessionStorage.getItem("token")}`,
            //     "day_time": `${_option.value}`,
            //     "show_id": `${_towspan.id}`,
            // }, "POST").then(re1 => {
            //     ajax("http://newshopapi.0melon0.cn/api/f_user/Qrcode", {
            //         "token": `${sessionStorage.getItem("token")}`,
            //         "id": `${re1.result.id}`,
            //     }, "GET").then(re2 => {
            //         console.log(re2);
            //         sessionStorage.setItem("img", `${re2.data.data}`);
            //         window.location.href = "./voucher.html";
            //     })
            // })
//2.0
            // bookday(data).then(re1 => {
            //     let data = {
            //         "token": `${sessionStorage.getItem("token")}`,
            //         "id": `${re1.result.id}`,
            //     }
            //     Qrcode(data).then(re2 => {
            //         console.log(re2);
            //         sessionStorage.setItem("img", `${re2.data.data}`);
            //         window.location.href = "./voucher.html";
            //     })
            // })
// 3.0
            let Data ={
                "token": `${sessionStorage.getItem("token")}`,
                "day_time": `${_option.value}`,
                "show_id": `${_towspan.id}`,
            }
            try {
                let re1 = await bookday(Data);
                let data = {
                    "token": `${sessionStorage.getItem("token")}`,
                    "id": `${re1.result.id}`,
                }
                let re2 = await Qrcode(data);
                console.log(re2);
                sessionStorage.setItem("img", `${re2.data.data}`);
                window.location.href = "./voucher.html";
            } catch (error) {
                console.log(error);
            }
           
        }

    })
    _three.addEventListener("click",async function () {
        if (_three.innerHTML == "可预约") {
            sessionStorage.setItem("day_time", `${_option.value}`);
            sessionStorage.setItem("cc_time", `${_threespan.innerHTML}`);
//1.0
            // ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
            //     "token": `${sessionStorage.getItem("token")}`,
            //     "day_time": `${_option.value}`,
            //     "show_id": `${_threespan.id}`,
            // }, "POST").then(re1 => {
            //     ajax("http://newshopapi.0melon0.cn/api/f_user/Qrcode", {
            //         "token": `${sessionStorage.getItem("token")}`,
            //         "id": `${re1.result.id}`,
            //     }, "GET").then(re2 => {
            //         console.log(re2);
            //         //  console.log(re2.data.data);
            //         sessionStorage.setItem("img", `${re2.data.data}`);
            //         window.location.href = "./voucher.html";
            //         // console.log(sessionStorage.getItem("img"));
            //     })
            // })
// 2.0
            // let data = {
            //     "token": `${sessionStorage.getItem("token")}`,
            //     "day_time": `${_option.value}`,
            //     "show_id": `${_threespan.id}`,
            // }
            // bookday(data).then(re1 => {
            //     let data = {
            //         "token": `${sessionStorage.getItem("token")}`,
            //         "id": `${re1.result.id}`,
            //     }
            //     Qrcode(data).then(re2 => {
            //         console.log(re2);
            //         //  console.log(re2.data.data);
            //         sessionStorage.setItem("img", `${re2.data.data}`);
            //         window.location.href = "./voucher.html";
            //         // console.log(sessionStorage.getItem("img"));
            //     })
            // })
// 3.0
            let Data ={
                "token": `${sessionStorage.getItem("token")}`,
                "day_time": `${_option.value}`,
                "show_id": `${_towspan.id}`,
            }
            try {
                let re1 = await bookday(Data);
                let data = {
                    "token": `${sessionStorage.getItem("token")}`,
                    "id": `${re1.result.id}`,
                }
                let re2 = await Qrcode(data);
                console.log(re2);
                sessionStorage.setItem("img", `${re2.data.data}`);
                window.location.href = "./voucher.html";
            } catch (error) {
                console.log(error);
            }
        }
    })
    _four.addEventListener("click",async function () {
        if (_four.innerHTML == "可预约") {
            sessionStorage.setItem("day_time", `${_option.value}`);
            sessionStorage.setItem("cc_time", `${_fourspan.innerHTML}`);
// 1.0
            // console.log(sessionStorage.getItem("day_time"));
            // ajax("http://newshopapi.0melon0.cn/api/f_user/bookday", {
            //     "token": `${sessionStorage.getItem("token")}`,
            //     "day_time": `${_option.value}`,
            //     "show_id": `${_fourspan.id}`,
            // }, "POST").then(re1 => {
            //     ajax("http://newshopapi.0melon0.cn/api/f_user/Qrcode", {
            //         "token": `${sessionStorage.getItem("token")}`,
            //         "id": `${re1.result.id}`,
            //     }, "GET").then(re2 => {
            //         console.log(re2);
            //         //  console.log(re2.data.data);
            //         sessionStorage.setItem("img", `${re2.data.data}`);
            //         window.location.href = "./voucher.html";
            //         // console.log(sessionStorage.getItem("img"));
            //     })
            // })
// 2.0
            // let data = {
            //     "token": `${sessionStorage.getItem("token")}`,
            //     "day_time": `${_option.value}`,
            //     "show_id": `${_threespan.id}`,
            // }
            // bookday(data).then(re1 => {
            //     let data = {
            //         "token": `${sessionStorage.getItem("token")}`,
            //         "id": `${re1.result.id}`,
            //     }
            //     Qrcode(data).then(re2 => {
            //         console.log(re2);
            //         //  console.log(re2.data.data);
            //         sessionStorage.setItem("img", `${re2.data.data}`);
            //         window.location.href = "./voucher.html";
            //         // console.log(sessionStorage.getItem("img"));
            //     })
            // })
// 3.0
            let Data ={
                "token": `${sessionStorage.getItem("token")}`,
                "day_time": `${_option.value}`,
                "show_id": `${_towspan.id}`,
            }
            try {
                let re1 = await bookday(Data);
                let data = {
                    "token": `${sessionStorage.getItem("token")}`,
                    "id": `${re1.result.id}`,
                }
                let re2 = await Qrcode(data);
                console.log(re2);
                sessionStorage.setItem("img", `${re2.data.data}`);
                window.location.href = "./voucher.html";
            } catch (error) {
                console.log(error);
            }
        }
    })
}